package com.myway.core.spring;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.io.Resource;

public final class SpringBeanUtil {
	private final static Logger log = Logger.getLogger(SpringBeanUtil.class);
	
	public static Object lookup(String id) {
		Object obj=ApplicationContextFactory.getApplicationContext().getBean(id);
		return obj;
	}
	
	/**
	 * 
	 * @param path ex: "classpath:seatmap/file/*.txt"
	 * @return
	 */
	public static Resource[] findFiles(String path) {
		Resource[] res;
		try {
			res = ApplicationContextFactory.getApplicationContext().getResources(path);
			return res;
		} catch (IOException e) {
			log.error(e);
		}
		return null;
	}
	
	public static MessageUtil getMessageUtil() {
		return (MessageUtil) SpringBeanUtil.lookup("messageUtil");
	}
	
	public static String getMessage(String key) {
		MessageUtil u = SpringBeanUtil.getMessageUtil();
		return u.getMessage(key);
	}
	
	public static String getMessage(String key, String... replace) {
		MessageUtil u = SpringBeanUtil.getMessageUtil();
		return u.getMessage(key, replace);
	}
	
    public static void publishEvent(ApplicationEvent ev) {
    	ApplicationContextFactory.getApplicationContext().publishEvent(ev);
    }
}
